package com.yuji.aacar.controller.mine.carmanage;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.base.Const;
import com.yuji.aacar.base.EventConst;
import com.yuji.aacar.base.UserCenter;
import com.yuji.aacar.bean.beans.Car;
import com.yuji.aacar.bean.beans.Room;
import com.yuji.aacar.bean.beans.Serial;
import com.yuji.aacar.bean.request.ManagePersonalCarRequest;
import com.yuji.aacar.bean.response.LoginResponse;
import com.yuji.aacar.bean.response.ManagePersonalCarResponse;
import com.yuji.aacar.bean.response.PersonalCarListResponse;
import com.yuji.aacar.controller.adapter.CarManagerAdapter;
import com.yuji.aacar.controller.cartype.ChooseCarTypeActivity;
import com.yuji.aacar.core.cores.event.CoreEventBusMessage;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.service.BaseServiceParams;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.swiplistview.SwipeMenu;
import com.yuji.aacar.widgets.swiplistview.SwipeMenuCreator;
import com.yuji.aacar.widgets.swiplistview.SwipeMenuItem;
import com.yuji.aacar.widgets.swiplistview.WSwipeMenuListView;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.yuji.aacar.service.BaseServiceMediator.MANAGE_PERSONAL_CAR;

public class CarManagementActivity extends BaseActivity {


    @BindView(R.id.back_login)
    TextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.car_manage_lv)
    WSwipeMenuListView carManageLv;
    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.car_manage_add_car_title)
    LinearLayout carManageAddCarTitle;
    @BindView(R.id.car_manage_add_car_btn)
    Button carManageAddCarBtn;
    @BindView(R.id.car_manage_add_car_layout)
    LinearLayout carManageAddCarLayout;
    @BindView(R.id.car_manage_no_car_layout)
    AutoRelativeLayout carManageNoCarLayout;

    private ArrayList<Car> list;
    private CarManagerAdapter adapter;

    private ArrayList<Car> datas;
    private int deletePosition;
    private String fromPage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_car_manage);
        ButterKnife.bind(this);
        initTitle(true, "车辆管理", null);

        fromPage = getIntent().getStringExtra("from");
        adapter = new CarManagerAdapter(CarManagementActivity.this, list);
        creator();
        carManageLv.setAdapter(adapter);
        carManageLv.setOnMenuItemClickListener(new WSwipeMenuListView.OnMenuItemClickListener() {   // ②
            @Override  // 设置监听事件
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:  // 在这里写入相应的额逻辑
                        deletePosition = position;
                        Car car = list.get(position);
                        requestDelete(car);
                        Toast.makeText(CarManagementActivity.this, "我删除了", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:  // 写入相应的逻辑
                        break;
                }
                return false;  // false : close the menu; true : not close the menu
            }
        });
        super.onCreate(savedInstanceState);
        carManageLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != list && list.size() > 0){
                    if (null != fromPage && fromPage.contentEquals(Const.FROM_WRITE_ORDER)){
                        Car car = list.get(position);
                        LoginResponse user = UserCenter.getCurrentUser();
                        if ( null != user){
                            String userId = user.getMemberId();
                            Serial serial = new Serial();
                            serial.setBrandId(car.getBrandId());
                            serial.setBrandName(car.getBrandName());
                            serial.setBrandUrl(car.getBrandUrl());
                            serial.setCarLibType(car.getCarLibType());
                            serial.setCarType(car.getCarType());
                            serial.setImgUrl(car.getBrandUrl());
                            serial.setPid(car.getPid());
                            serial.setSeriesName(car.getSeriesName());
                            serial.setSeriesType(car.getSeriesType());
                            UserCenter.setFirstCar(userId,serial);
                            CoreEventCenter.postObject(EventConst.SELECT_CAR_OVER, serial);
                        }
//                        setResult(1, getIntent().putExtra("car", car)); //回到订单填写

                        finish();
                    }
                }
            }
        });
    }

    @Override
    public void alreadyBindBaseViewModel() {
        super.alreadyBindBaseViewModel();
    }

    @Override
    public void onResume() {
        super.onResume();
        request();
    }

    private void request() {
        requestParams = BaseServiceParams.getPersonalCarList();
        doTask(BaseServiceMediator.GET_PERSONAL_CAR_LIST);
    }

    private void requestDelete(Car car) {
        ManagePersonalCarRequest request = new ManagePersonalCarRequest();
        request.setPid(car.getPid());
        request.setSeriesType(car.getSeriesType());
        request.setSeriesName(car.getSeriesName());
        request.setCarLibType(car.getCarLibType());
        request.setBrandUrl(car.getBrandUrl());
        request.setBrandId(car.getBrandId());
        request.setCarNo(car.getCarNo());
        request.setCarType(car.getCarType());
        request.setOperatorFlag("3");
        requestParams = BaseServiceParams.managePersonalCar(request);
        doTask(MANAGE_PERSONAL_CAR);
    }

    private void creator() {
        SwipeMenuCreator creator = new SwipeMenuCreator() {    // SwipeMenuCreator对象的复写方法进行属性设置
            @Override
            public void create(SwipeMenu menu) {
                // 创建删除
                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xFB, 0x55, 0x57)));//FB5557
                deleteItem.setWidth(315);
                deleteItem.setTitle("删除");
                deleteItem.setTitleColor(Color.WHITE);
                deleteItem.setTitleSize(18);
                menu.addMenuItem(deleteItem);
            }
        };
        carManageLv.setMenuCreator(creator); // listview 设置 添加creator
        carManageLv.setSwipeDirection(carManageLv.DIRECTION_LEFT); // 设置滑出方式为左滑

    }


    @OnClick(R.id.car_manage_add_car_btn)
    public void onClick() {
        presentController(ChooseCarTypeActivity.class);
    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.MANAGER_PERSONAL_CAR_LIST) {
            PersonalCarListResponse carListResponse = (PersonalCarListResponse) response.getResponse();
            list = carListResponse.getInfo();
            adapter.updateData(list);
        } else if (response.getRequestCode() == BaseServiceMediator.MANAGER_PERSONAL_CAR_DELETE) {
            ManagePersonalCarResponse personalCarResponse = (ManagePersonalCarResponse) response.getResponse();
            if (personalCarResponse.isSuccess()) {
                list.remove(deletePosition);
                adapter.notifyDataSetChanged();
            }
        }

        if (null == list || list.size() == 0){
            carManageNoCarLayout.setVisibility(View.VISIBLE);
        } else {
            carManageNoCarLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void handleMessage(CoreEventBusMessage message) {
        super.handleMessage(message);
        if (message.getMessageCode().contentEquals(String.valueOf(EventConst.SELECT_ROOM_OVER))){
            Room roomCache = (Room) message.getMessageObjects();
           if (null != roomCache){
               this.finish();
           }
        } else if (message.getMessageCode().contentEquals(String.valueOf(EventConst.FIRST_DEFAULT_SERIAL_FINISH))){
            if (null != fromPage && fromPage.contentEquals(Const.FROM_WRITE_ORDER)){
                this.finish();
            }
        }
    }
}
